﻿using System;
using System.Runtime.InteropServices;
using SpeedikonCom;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace SOM.Lists
	{
  [ComDefaultInterface(typeof(SOM.Interfaces.Lists.IOpeningList)), ClassInterface(ClassInterfaceType.None)]
  [ComSourceInterfaces(typeof(SOM.Interfaces.Lists.IOpeningList))]
  [Serializable()]
  public class OpeningList : List<SOM.Objects.Opening>, SOM.Interfaces.Lists.IOpeningList
  {
    #region Typen-Konstanten
    [ComVisible(false)]
    const string sOeffnungName = "Oeffnung";
    [ComVisible(false)]
    const string sOeffnungZellName = "Opening";
    #endregion


    #region globale Variablen
    [ComVisible(false)]
    Dictionary<string, int> ldObjektTyp = new Dictionary<string, int>();
    [ComVisible(false)]
    List<string> scObjektName = new List<string>();
    [ComVisible(false)]
    Dictionary<string, string> sdObjektZellName = new Dictionary<string, string>();
    [ComVisible(false)]
    Dictionary<string, Dictionary<string, string>> sddObjektAttribut = new Dictionary<string, Dictionary<string, string>>();
    #endregion

    public void SendToDatabase(string server, string table, string SchemeFilePath = "")
    {
      SOM.Helper.Methods.SendToDatabase<SOM.Objects.Opening>(this, server,table,SchemeFilePath);
    }
    public void GetFromDatabase(string server, string table, string SchemeFilePath = "")
    {
      var tmp = (SOM.Interfaces.Lists.IOpeningList)SOM.Helper.Methods.GetFromDatabase<SOM.Objects.Opening>(server,-1, table,SchemeFilePath);
      foreach (System.Reflection.PropertyInfo p in this.GetType().GetProperties())
      {
        try { this.GetType().GetProperty(p.Name).SetValue(this, tmp.GetType().GetProperty(p.Name).GetValue(tmp, null), null); }
        catch { }
      }
    }

    private Dictionary<string, int> dParamInt = new Dictionary<string, int>();
    private Dictionary<string, int[]> dParamIntA = new Dictionary<string, int[]>();
    private Dictionary<string, double[]> dParamDoubleA = new Dictionary<string, double[]>();
    private Dictionary<string, double> dParamDouble = new Dictionary<string, double>();
    private Dictionary<string, string> dParamText = new Dictionary<string, string>();
    private Dictionary<string, string[]> dParamTextA = new Dictionary<string, string[]>();
    private Dictionary<string, int> dParamSwitch = new Dictionary<string, int>();


    [ComVisible(true)]
    public SOM.Interfaces.Objects.IOpening GetOpening(int index)
    {
      return this[index];
    }


    [ComVisible(true)]
    public SOM.Interfaces.Objects.IOpening GetOpeningFromID(int ID)
    {
      foreach (SOM.Objects.Opening w in this)
      {
        if (w.ID == ID)
        {
          return w;
        }
      }
      return null;
    }





    #region COM-GUIDs
    // Diese GUIDs stellen die COM-Identität für diese Klasse 
    // und ihre COM-Schnittstellen bereit. Wenn Sie sie ändern, können vorhandene 
    // Clients nicht mehr auf die Klasse zugreifen.
    [ComVisible(false)]
    public const string ClassId = "7ADB04DD-D576-48CB-9E17-5CD0CF9D6D8F";
    [ComVisible(false)]
    public const string InterfaceId = "6B953FC2-6035-45D7-9130-9242A28B244A";
    [ComVisible(false)]
    public const string EventsId = "935F856D-3E9E-4142-95AF-42708E7076D2";
    #endregion



    /// <summary>
    /// Erstellt ein neues CNT Objekt (zZ Wandliste)
    /// </summary>
    /// <author>Steve</author>
    /// <creationdate>02.05.2012</creationdate>
    /// <lasteditedby>Steve</lasteditedby>
    /// <lasteditdate>02.05.2012</lasteditdate>
    /// <remarks></remarks>
    public OpeningList()
    {
      //initOeffnung();
    }

    /// <summary>
    /// Gibt die Anzahl der gefunden Wände zurück
    /// </summary>
    /// <value>Nur lesbar</value>
    /// <author>Steve</author>
    /// <creationdate>02.05.2012</creationdate>
    /// <lasteditedby>Steve</lasteditedby>
    /// <lasteditdate>02.05.2012</lasteditdate>
    /// <remarks></remarks>
    public new int Count
    {
      get { return base.Count; }
      set
      {

      }
    }

    /// <summary>
    /// Durchsucht die aktuelle Speedikon Zeichnung nach Wänden und speichert diese als Wand-Objekt in einer internen Liste
    /// </summary>
    /// <param name="pfad">Wird hier ein Pfad übergeben, wird die gesamte Wandliste als XML an dem angegebenen Pfad gespeichert</param>
    /// <param name="full">Gibt an ob alle Informationen oder nur eine "Light" Version davon gespeichert wird</param>
    /// <param name="debug">Führt die Sub im Debugmodus aus</param>
    /// <author>Steve</author>
    /// <creationdate>02.05.2012</creationdate>
    /// <lasteditedby>Steve</lasteditedby>
    /// <lasteditdate>02.05.2012</lasteditdate>
    /// <example><code lang="VB.NET">Dim obj As New elkmenu.helper
    /// Dim wand As New elkmenu.wand
    /// Dim i As integer
    /// obj.ScanWalls
    /// 
    /// For i = 0 to obj.Anzahl - 1
    /// Set wand = obj.GetWall(i)
    /// MsgBox(wand.A_PROTOTYPE)
    /// next
    /// </code></example>
    /// <remarks></remarks>
    public void ScanOpenings(string pfad)
    {
      this.Clear();

      List<string> sdObjektName = new List<string>();
      Dictionary<string, string> sddOjektAttribut = new Dictionary<string, string>();

      Dictionary<string, string> sdAttribute = new Dictionary<string, string>();
      IEZ_sp_Opening clOpening = new IEZ_sp_Opening();
      IEZ_sp_Object clObject = new IEZ_sp_Object();


      int iCntWindows;

      int[] iIdWindows;

      short iLop;
      int iRet;


      iLop = 0;
      iIdWindows = null;

      clObject.GetList((int)5, out iCntWindows, out  iIdWindows, out iRet);


      if (iCntWindows > 0)
      {

        for (iLop = 0; iLop <= iCntWindows - 1; iLop++)
        {


          // Liste mit Userattributen der Wand holen
          // ----------------------------------------

          SOM.Objects.Opening win = new SOM.Objects.Opening();
          win.LoadOpening(iIdWindows[iLop]);


          this.Add(win);


        }
        if (pfad != "")
        {
          string[] tmp = pfad.Split('\\');
          if (System.IO.Directory.Exists(pfad.Substring(0, pfad.Length - tmp[tmp.GetUpperBound(0)].Length)))
          {

            this.Serialisieren(pfad);

          }
          else
          {
            throw new Exception("Pfad nicht gefunden!");

          }
        }
      }
    }







    /// <summary>
    /// Speichert das Objekt in eine XML-Datei
    /// </summary>
    /// <param name="pfad">Vollständiger Pfad zur XML-Datei</param>
    /// <author>Steve</author>
    /// <creationdate>02.05.2012</creationdate>
    /// <lasteditedby>Steve</lasteditedby>
    /// <lasteditdate>02.05.2012</lasteditdate>
    /// <example><code lang="VB.NET">Dim obj As New elkmenu.helper
    /// obj.ScanWalls
    /// 
    /// obj.serialisieren("c:\Test.xml")</code></example>
    /// <remarks></remarks>
    public void Serialisieren(string pfad)
    {

      //object obj = this;
      foreach (SOM.Objects.Opening w in this)
      {
        w.SetPropertiesReadOnly(false);
      }

      XmlSerializer XML = new XmlSerializer(typeof(List<SOM.Objects.Opening>));

      System.IO.FileStream FS = new System.IO.FileStream(pfad, System.IO.FileMode.Create);
      XML.Serialize(FS, this);
      FS.Close();


      //Dim XML As New XmlSerializer(MyBase.GetType)

      //Dim FS As New FileStream(pfad, FileMode.Create)
      //XML.Serialize(FS, obj)
      //FS.Close()
      foreach (SOM.Objects.Opening w in this)
      {
        w.SetPropertiesReadOnly(true);
      }
    }




    /// <summary>
    /// Lädt das Objekt aus einer XML-Datei
    /// </summary>
    /// <param name="pfad">Vollständiger Pfad zur XML-Datei</param>
    /// <author>Steve</author>
    /// <creationdate>02.05.2012</creationdate>
    /// <lasteditedby>Steve</lasteditedby>
    /// <lasteditdate>02.05.2012</lasteditdate>
    /// <example><code lang="VB.NET">Dim obj As New elkmenu.helper
    /// obj.deserialisieren("c:\Test.xml")
    /// 
    /// Dim w As New elkmenu.wand
    /// 
    /// For i = 0 to obj.Anzahl - 1
    /// Set w = obj.GetWall(i)
    /// MsgBox(wand.A_PROTOTYPE)
    /// next</code></example>
    /// <remarks></remarks>
    public bool Deserialisieren(string pfad)
    {
      bool ret = true;
      Type T = base.GetType();



      XmlSerializer XML = new XmlSerializer(T);
      System.IO.FileStream fs = new System.IO.FileStream(pfad, System.IO.FileMode.Open);
      try
      {
        Object obj = XML.Deserialize(fs);

        this.Clear();
        foreach (SOM.Objects.Opening w in (List<SOM.Objects.Opening>)obj)
        {
          this.Add(w);
        }
        //foreach (System.Reflection.PropertyInfo eigenschaft in obj.GetType().GetProperties())
        //{
        //  if (eigenschaft.CanWrite)
        //  {
        //    this.GetType().GetProperty(eigenschaft.Name).SetValue(this, this.GetType().GetProperty(eigenschaft.Name).GetValue(obj, null), null);
        //  }
        //}
      }
      catch (Exception ex)
      {
        ret = false;
        System.Windows.Forms.MessageBox.Show("Fehler in der Datenstruktur! " + ex.Message);
      }
      finally
      {
        fs.Close();
      }
      return ret;
    }
    
  }
}
